package com.jiangxiaofei.citycloud.mvp;

import android.content.Context;
import android.widget.Toast;

import com.jiangxiaofei.citycloud.bean.BannerBean;
import com.jiangxiaofei.citycloud.bean.RealUserBean;
import com.jiangxiaofei.citycloud.bean.UserInfoBean;
import com.jiangxiaofei.citycloud.httpUtil.APIUrl;
import com.jiangxiaofei.citycloud.httpUtil.HttpResult;
import com.jiangxiaofei.citycloud.httpUtil.OnLoadDataIm;
import com.jiangxiaofei.citycloud.httpUtil.RetrofitUtils;
import com.jiangxiaofei.citycloud.param.AddRealidParam;
import com.jiangxiaofei.citycloud.param.PhoneCodeParam;
import com.jiangxiaofei.citycloud.param.UserInfoParam;
import com.jiangxiaofei.citycloud.param.UserRegistParam;
import com.jiangxiaofei.citycloud.param.UserStateParam;
import com.jiangxiaofei.citycloud.util.L;
import com.jiangxiaofei.citycloud.util.StringUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by JiangXFei on 2017/8/23.
 */

public class MainModel implements MainConstant.UpLoadPhotoModel,
        MainConstant.AddUserRealInfoModel, MainConstant.GetRealUserInfoModel
        , MainConstant.UserRegistModel, MainConstant.PhoneIsRepeatModel
        , MainConstant.UserLoginModel, MainConstant.GetBannerModel
        , MainConstant.GetPhoneCodeModel, MainConstant.UpdatePhoneModel
        , MainConstant.UpdatePwdModel, MainConstant.UpdateUserNameModel
        , MainConstant.UserRepeatNameModel, MainConstant.FindUserPwdModel {

    /**
     * 上传图片
     *
     * @param mContext
     * @param file
     * @param callBack
     */
    @Override
    public void uploadPhoto(final Context mContext, File file, final OnLoadDataIm callBack) {
        List<File> files = new ArrayList<>();
        files.add(file);
        new RetrofitUtils(mContext, "").sendMultipart(APIUrl.API_URL + "/comfun/uploadimg", null, "file", files)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable throwable) {
//                        ToolUtils.setError(throwable.toString(), "upLoadHead");
//                        ToastUtils.toastShort(mContext, "上传失败");
                        callBack.onFailure(throwable.toString(), 500);
                    }

                    @Override
                    public void onNext(String stringHttpResult) {
                        JSONObject mJsonObject = null;
                        try {
                            L.i("--------------------------shangchuan" + stringHttpResult);
                            if (StringUtil.isNotNull(stringHttpResult)) {
                                mJsonObject = new JSONObject(stringHttpResult);
                                int code = mJsonObject.getInt("code");
                                String message = mJsonObject.getString("msg").trim();
                                String dataStr = mJsonObject.getString("data").trim();
                                callBack.onSuccess(dataStr, message, code);
                            } else {
                                callBack.onFailure("网络错误", 500);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    /**
     * 上传用户真实信息
     */
    @Override
    public void addUserRealInfo(final Context mContext, AddRealidParam realidParam, final OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .addRealid(realidParam.getUserId(), realidParam.getSex(), realidParam.getAge(), realidParam.getPlace(), realidParam.getRealName(), realidParam.getRealNumber())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<String> stringHttpResult) {
                        if (stringHttpResult.getCode() == 1) {
                            callback.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                        } else {
                            Toast.makeText(mContext, "网络异常", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    @Override
    public void getRealUserInfo(Context mContext, int userId, final OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext).getRealUserInfo(userId)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<RealUserBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<RealUserBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 用户注册
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void registUser(Context mContext, UserRegistParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .userRegist(param.getPhoneNumber(), param.getPassword(), param.getCode(),param.getState())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<String> stringHttpResult) {
                        if (stringHttpResult.getCode() == 1) {
                            callback.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                        } else {
                            Toast.makeText(mContext, "注册异常，请检查网络", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    /**
     * 验证用户手机号
     *
     * @param mContext
     * @param phoneNum
     * @param callback
     */
    @Override
    public void phoneRepeat(Context mContext, String phoneNum, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .isPhoneRepeat(phoneNum)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, "网络异常，请稍后在试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(HttpResult<String> stringHttpResult) {
                        callback.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                    }
                });
    }

    /**
     * 用户登陆
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void userLogin(Context mContext, UserInfoParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .userLogin(param.getName(), param.getPassword())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<UserInfoBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<UserInfoBean>> userInfoBeanHttpResult) {
                        if (userInfoBeanHttpResult.getCode() == 1) {
                            callback.onSuccess(userInfoBeanHttpResult.getData(), userInfoBeanHttpResult.getMessage(), userInfoBeanHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 获取banner轮播图
     *
     * @param mContext
     * @param id
     * @param callback
     */
    @Override
    public void getBanner(Context mContext, int id, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getBannerImg(id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<BannerBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<BannerBean>> objectHttpResult) {
                        if (objectHttpResult.getCode() == 1) {
                            callback.onSuccess(objectHttpResult.getData(), objectHttpResult.getMessage(), objectHttpResult.getCode());
                        } else {
                            callback.onFailure(objectHttpResult.getMessage(), objectHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 获取手机验证码
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void getPhoneCode(Context mContext, PhoneCodeParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getPhoneCode(param.getPhoneNum(), param.getState())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<String> stringHttpResult) {
                        if (stringHttpResult.getCode() == 1) {
                            callback.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                        } else {
                            callback.onFailure(stringHttpResult.getMessage(), stringHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 修改密码
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void updatePwd(Context mContext, UserStateParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .updateUserPwd(param.getPhoneNum()
                        , param.getState()
                        , param.getPassword()
                        , param.getCode()
                        , param.getId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<String> stringHttpResult) {
                        if (stringHttpResult.getCode() == 1) {
                            callback.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                        } else {
                            callback.onFailure(stringHttpResult.getMessage(), stringHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 修改用户手机号
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void updatePhone(Context mContext, UserStateParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .updatePhoneNum(param.getPhoneNum(), param.getState(), param.getCode(), param.getId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<Object>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<Object> objectHttpResult) {
                        if (objectHttpResult.getCode() == 1) {
                            callback.onSuccess(objectHttpResult.getData()
                                    , objectHttpResult.getMessage()
                                    , objectHttpResult.getCode());
                        } else {
                            callback.onFailure(objectHttpResult.getMessage(), objectHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 修改用户名称
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void updateUserNmae(Context mContext, UserStateParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .updateUserName(param.getName(), param.getId(), param.getPhoneNum())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<Object>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<Object> objectHttpResult) {
                        if (objectHttpResult.getCode() == 1) {
                            callback.onSuccess(objectHttpResult.getData(), objectHttpResult.getMessage(), objectHttpResult.getCode());
                        } else {
                            callback.onFailure(objectHttpResult.getMessage(), objectHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 验证用户名是否重复
     *
     * @param mContext
     * @param name
     * @param callback
     */
    @Override
    public void RepeatName(Context mContext, String name, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .updateRepeatName(name)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<Object>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<Object> objectHttpResult) {
                        if (objectHttpResult.getCode() == 1) {
                            callback.onSuccess(objectHttpResult.getData(), objectHttpResult.getMessage(), objectHttpResult.getCode());
                        } else {
                            callback.onFailure(objectHttpResult.getMessage(), objectHttpResult.getCode());
                        }

                    }
                });
    }

    /**
     * 找回密码
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void findPwd(Context mContext, UserStateParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .findUserPwd(param.getPhoneNum(), param.getState(), param.getCode(), param.getPassword())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<Object>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<Object> objectHttpResult) {
                        if (objectHttpResult.getCode() == 1) {
                            callback.onSuccess(objectHttpResult.getData(), objectHttpResult.getMessage(), objectHttpResult.getCode());
                        } else {
                            callback.onFailure(objectHttpResult.getMessage(), objectHttpResult.getCode());
                        }
                    }
                });
    }
}
